﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Controls;

namespace StatusPlan3.UC
{
    /// <summary>
    /// Interaction logic for UCFiles.xaml
    /// </summary>
    public partial class UCFiles : UserControl
    {
        AttachedFiles files;
        public UCTask ucTask;
        public UCTasks ucProject;
       
        public UCFiles()
        {
            InitializeComponent();
        }

        public UCFiles(AttachedFiles afs)
        {
            files = afs;
            InitializeComponent();

            if(afs.Count > 0)
                Button_DownloadAll.IsEnabled = true;
            else
                Button_DownloadAll.IsEnabled = false;

            foreach (RadTreeViewItem rtvi in buildTreeView(afs))
            {
                File_TreeView.Items.Add(rtvi);
            }
        }

        public List<RadTreeViewItem> buildTreeView(AttachedFiles allFiles)
        {
            List<RadTreeViewItem> tempList = new List<RadTreeViewItem>();

            var folder = (from f in allFiles where f.isFolder == true select f);

            foreach (AttachedFile file in folder) 
            {
                RadTreeViewItem rtvi = new RadTreeViewItem();
                rtvi.Header = file.fileName;
                rtvi.DefaultImageSrc = new BitmapImage(new Uri("/Images/folder.png",UriKind.Relative));

                RadContextMenu rcm = buildFileContextMenu(file);
                RadContextMenu.SetContextMenu(rtvi, rcm);
                rtvi.ToolTip = getTooltip(file);

                rtvi.Expanded += new EventHandler<Telerik.Windows.RadRoutedEventArgs>(rtvi_Expanded);
                rtvi.Collapsed += new EventHandler<Telerik.Windows.RadRoutedEventArgs>(rtvi_Collapsed);
                foreach (RadTreeViewItem rtvi2 in buildTreeView(file.attachedFiles))
                {
                    rtvi.Items.Add(rtvi2);
                }
                tempList.Add(rtvi);
            }

            var files2 = (from f in allFiles where f.isFolder == false select f);

            foreach (AttachedFile file in files2)
            {
                RadTreeViewItem rtvi = new RadTreeViewItem();
                rtvi.Header = file.fileName + "." + file.fileType;
                rtvi.DefaultImageSrc = new BitmapImage(new Uri("/Images/search.png", UriKind.Relative));
                rtvi.Tag = file;
                rtvi.ToolTip = getTooltip(file);

                RadContextMenu rcm = buildFileContextMenu(file);
                RadContextMenu.SetContextMenu(rtvi, rcm);
                rtvi.DoubleClick += new EventHandler<Telerik.Windows.RadRoutedEventArgs>(rtvi_Click);
                tempList.Add(rtvi);

            }

            return tempList;
        }

        private String getTooltip(AttachedFile af)
        {
            String returnValue = "";
            returnValue += af.fileName;
            if(af.fileType != null && af.fileType.Length > 0)
                returnValue += "." + af.fileType;
            returnValue += "\n";
            returnValue += "Created by: " + af.createdBy.fullName + " (" + af.dateAdded.ToString("dd-MM-yyyy HH:mm") + ")";
            returnValue += "\n";
            returnValue += "Size: " + af.size.toGoodSize();

            return returnValue;
        }

        private RadContextMenu buildFileContextMenu(AttachedFile af)
        {
            RadContextMenu rcm = new RadContextMenu();

            RadMenuItem rmi = new RadMenuItem();

            if (!af.isFolder)
            {
                rmi = new RadMenuItem();
                rmi.Header = "Lat upp";
                rmi.Tag = af;
                rmi.Click += new Telerik.Windows.RadRoutedEventHandler(rmi_Open_Click);
                rcm.Items.Add(rmi);

                rmi = new RadMenuItem();
                rmi.Header = "Goym";
                rmi.Tag = af;
                rmi.Click += new Telerik.Windows.RadRoutedEventHandler(rmi_Download_Click);
                rcm.Items.Add(rmi);

                rmi = new RadMenuItem();
                rmi.IsSeparator = true;
                rcm.Items.Add(rmi);
            }
            else
            {
                rmi = new RadMenuItem();
                rmi.Header = "Goym sum ZIP";
                rmi.Tag = af;
                rmi.Click += new Telerik.Windows.RadRoutedEventHandler(rmi_Download_Click);
                rcm.Items.Add(rmi);

                rmi = new RadMenuItem();
                rmi.IsSeparator = true;
                rcm.Items.Add(rmi);
            }

            rmi = new RadMenuItem();
            rmi.Header = "Strika";
            rmi.Tag = af;
            rmi.Click += new Telerik.Windows.RadRoutedEventHandler(rmi_Click);
            rcm.Items.Add(rmi);

            rmi = new RadMenuItem();
            rmi.Header = "Rename";
            rcm.Items.Add(rmi);

            return rcm;
        }

        private void deleteFile(AttachedFile af)
        {
            String msg = "";

            if (af.isFolder)
                msg = "Ert tú vís/ur í at tú vilt strika mappuna: " + af.fileName + " og allar undirfílur?";
            else
                msg = "Ert tú vís/ur í at tú vilt strika fíluna: " + af.fileName + "." + af.fileType + "?";

            if (MessageBox.Show(msg, "Strika", MessageBoxButton.YesNoCancel, MessageBoxImage.Question) == MessageBoxResult.Yes)
            {
                if (ucTask != null)
                {
                    StatusPlanData.statusPlanHandler.removeTaskFile(af, ucTask.task);
                    ucTask.refreshFrame();
                }
                else if (ucProject != null)
                {
                    StatusPlanData.statusPlanHandler.removeProjectFile(af, ucProject.project);
                    ucProject.refreshFrame();
                }
            }
        }

        void rmi_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            RadMenuItem rmi = sender as RadMenuItem;
            AttachedFile af = rmi.Tag as AttachedFile;
            deleteFile(af);
        }

        void rmi_Open_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            RadMenuItem rmi = sender as RadMenuItem;
            AttachedFile af = rmi.Tag as AttachedFile;

            StatusPlanData.statusPlanHandler.openFile(af);
        }

        void rmi_Download_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            RadMenuItem rmi = sender as RadMenuItem;
            AttachedFile af = rmi.Tag as AttachedFile;

            StatusPlanData.statusPlanHandler.downloadFile(af);
        }

        void rtvi_Collapsed(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            RadTreeViewItem rtvi = sender as RadTreeViewItem;
            rtvi.DefaultImageSrc = new BitmapImage(new Uri("/Images/folder.png", UriKind.Relative));
        }

        void rtvi_Expanded(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            RadTreeViewItem rtvi = sender as RadTreeViewItem;
            rtvi.DefaultImageSrc = new BitmapImage(new Uri("/Images/folder_open.png", UriKind.Relative));

        }

        void rtvi_Click(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            RadTreeViewItem rtvi = sender as RadTreeViewItem;
            AttachedFile af = rtvi.Tag as AttachedFile;

            StatusPlanData.statusPlanHandler.openFile(af);
        }

        private void File_TreeView_Drop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            if (files != null &&files.Count() > 0)
            {
                AttachedFiles afs = StatusPlanData.statusPlanHandler.buildFilesFromString(files);

                String content = "";
                if(files.Count() == 1) 
                    content += "Vil tú leggja hesa fílu (" + afs.getTotalSize().toGoodSize() + ") aftrat:\n\n";
                else
                    content += "Vil tú leggja allar hesar " + afs.getTotalNumberOfFiles() + " fílur aftrat(" + afs.getTotalSize().toGoodSize() + ") :\n\n";

                if (MessageBox.Show(content, "Legg fílur aftrat", MessageBoxButton.YesNoCancel, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {

                    if (ucTask != null)
                    {
                        StatusPlanData.statusPlanHandler.addTaskFile(afs, ucTask.task);
                        ucTask.refreshFrame();
                    }
                    else if (ucProject != null)
                    {
                        StatusPlanData.statusPlanHandler.addProjectFile(afs, ucProject.project);
                        ucProject.refreshFrame();
                    }
                }
            }           
        }

        private void File_TreeView_DragEnded(object sender, RadTreeViewDragEndedEventArgs e)
        {

        }

        private void RadButton_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog openDialog = new System.Windows.Forms.OpenFileDialog();
            openDialog.Filter = "Allar fílur|*.*";
            openDialog.Multiselect = false;
            var dialogResult = openDialog.ShowDialog();

            if (dialogResult == System.Windows.Forms.DialogResult.OK)
            {
                String pathToFile = openDialog.FileName;
                string[] list = new string[] { pathToFile };
                AttachedFiles afs = StatusPlanData.statusPlanHandler.buildFilesFromString(list);
                StatusPlanData.statusPlanHandler.addTaskFile(afs, ucTask.task);
                ucTask.refreshFrame();
            }
        }

        private void Button_DeleteFile_Click(object sender, RoutedEventArgs e)
        {
            RadTreeViewItem rtvi = File_TreeView.SelectedItem as RadTreeViewItem;
            if (rtvi != null)
            {
                AttachedFile af = rtvi.Tag as AttachedFile;
                if (af != null)
                    deleteFile(af);
            }


        }

        private void Button_DownloadAll_Click(object sender, RoutedEventArgs e)
        {
            if(ucTask != null)
                StatusPlanData.statusPlanHandler.taskDownloadAllFiles(ucTask.task);
            else if(ucTask != null)
                StatusPlanData.statusPlanHandler.projectDownloadAllFiles(ucProject.project);
        }

        private void File_TreeView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            RadTreeViewItem rtvi = File_TreeView.SelectedItem as RadTreeViewItem;
            Button_DeleteFile.IsEnabled = false;

            if (rtvi != null)
            {
                Button_DeleteFile.IsEnabled = true;
            }
        }
    }
}
